package org.fintool.functional;

import java.util.ArrayList;

import org.fintool.beans.Scrip;
import org.fintool.beans.Tick;
import org.fintool.common.MathUtil;

public class TransfromData {

	public final static Tick transformLinetoTicker(final String thisline,final String nextLine){

		Tick tick=new Tick();

		String thisLineArray[]=null;

		String nextLineArray[]=null;

		/*14,41.12,41.64,40.99,41.49,2136354
		15,40.8,40.83,40.05,40.28,3640339
		COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME*/


		if(thisline!=null&&thisline.indexOf(',')>0){
			thisLineArray=thisline.split(",");

			if(thisLineArray.length==6){
			tick.setDayMark(thisLineArray[0]);
			tick.setClose(Double.parseDouble(thisLineArray[1]));
			tick.setHigh(Double.parseDouble(thisLineArray[2]));
			tick.setLow(Double.parseDouble(thisLineArray[3]));
			tick.setOpen(Double.parseDouble(thisLineArray[4]));
			tick.setVolume(Long.parseLong(thisLineArray[5]));

			tick.setPercentageDiff(MathUtil.getPercentChange(tick.getOpen(), tick.getClose()));
			tick.setPercentageDiffOpenMax(MathUtil.getPercentChange(tick.getOpen(), tick.getHigh()));
			tick.setPercentageDiffOpenLow(MathUtil.getPercentChange(tick.getOpen(), tick.getLow()));

			tick.setPercentageDiffMaxDay(MathUtil.getPercentChange(tick.getLow(), tick.getHigh()));


			if(nextLine!=null&&nextLine.length()>0&&nextLine.indexOf(',')>0){

				nextLineArray=nextLine.split(",");

				tick.setPercentageDiffPrevGainOnClose(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[1]),tick.getClose()));
				tick.setPercentageDiffPrevGainMax(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[1]),tick.getHigh()));

				tick.setPercentageDiffAtOpen(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[1]),tick.getOpen()));

				tick.setPercentageDiffPrevNegativeMax(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[1]),tick.getLow()));

				tick.setPercentageDiffLastLowVsOpen(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[3]),tick.getOpen()));

				tick.setPercentageDiffLastMaxVsOpen(MathUtil.getPercentChange(Double.parseDouble(nextLineArray[2]),tick.getOpen()));

			}


			}



		}





		return tick;
	}





	public final static Tick transformLinetoTicker(final String thisline){
		return transformLinetoTicker(thisline,null);
	}


	public static final Scrip  makeScripFromLines(final ArrayList alLinedList,final String stock,final String exchange) throws Exception{

		Scrip retScrip=null;
		ArrayList alTickers=null;
		Tick currenTick=null;


		if(alLinedList!=null&&exchange!=null&&stock!=null){
			retScrip=new Scrip();
			retScrip.setName(stock);
			retScrip.setExchange(exchange);

			alTickers=new ArrayList(alLinedList.size());
			for(int i=0;i<alLinedList.size();i++){
				if(i==alLinedList.size()-1){
					currenTick=transformLinetoTicker(alLinedList.get(i).toString());
				}else{
					currenTick=transformLinetoTicker(alLinedList.get(i).toString(),alLinedList.get(i+1).toString());
				}


				alTickers.add(currenTick);

			}

			retScrip.setTickers(alTickers);




		}//EOIF



		return retScrip;

	}

	/**
	 * @param alRawData
	 * @return
	 * For now no date change just reversing so that most latest comes on top
	 */
	public final static ArrayList transRawtoProperDates(final ArrayList alRawData){

		ArrayList alTransFormList=null;

		if(alRawData!=null&&alRawData.size()>0){

			alTransFormList=new ArrayList(alRawData.size());

		for (int i=alRawData.size()-1;i>=0;i--){

			//System.out.println(alRawData.get(i));
			alTransFormList.add(alRawData.get(i));



		}
		//Loop
		}else{
			System.out.println("Raw Array has no data ");
		}




		return alTransFormList;
	}

}
